package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LogoutAction extends Action {
	
	private Logger logger = Logger.getLogger(LogoutAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session = request.getSession(false);
		
		if (session != null && (System.currentTimeMillis() - session.getCreationTime() > 2000)) {
			logger.info(String.format("Removing the user from the HttpSession=%s", session));
			session.removeAttribute("user");
			return mapping.findForward("success");
		} else {
			request.setAttribute("errorDescription", "La sesion NO existia !!??");
			return mapping.findForward("errorSessionDidntExist");
		}
	}

}
